package com.example.pattern.utils

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.icu.number.Scale
import android.util.TypedValue
import android.view.View
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

fun View.dp2px(dp:Int):Int{
    return (context.resources.displayMetrics.density*dp).toInt()
}
fun Context.dp2px(dp:Int):Int{
    return (resources.displayMetrics.density*dp).toInt()
}

fun Context.sp2px(sp:Int):Float{
 return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,sp.toFloat(),resources.displayMetrics)
}
suspend fun convertViewToBitmap(view: View):Bitmap= withContext(Dispatchers.IO){
    //创建一个空的画布Bitmap
    val bitmap = Bitmap.createBitmap(view.width,view.height,Bitmap.Config.ARGB_8888)
    val canvas = Canvas(bitmap)
    view.draw(canvas)
    bitmap
}
suspend fun scaleBitmap(mBitmap: Bitmap,scale: Float):Bitmap = withContext(Dispatchers.IO){
    Bitmap.createScaledBitmap(
        mBitmap,
        (mBitmap.width*scale).toInt(),
        (mBitmap.height*scale).toInt(),
        true
    )
}